package org.senf.outerspace.engine.pathing;

import org.eclipse.swt.graphics.Point;

public class Node {

	public final static int N = 1;
	public final static int NE = 2;
	public final static int E = 3;
	public final static int SE = 4;
	public final static int S = 5;
	public final static int SW = 6;
	public final static int W = 7;
	public final static int NW = 8;
	
	public Node parent;
	
	public int x;
	public int y;
	
	public int g;
	public int h;
	
	public Node child(int direction) {
		return child(this, direction);
	}
	
	public int getCost() {
		return g+h;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Node other = (Node) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "Node (" + x + "," + y + ") = " + g + " + " + h;
	}

	public static Node create(Point p) {
		Node node = new Node();
		node.x = p.x;
		node.y = p.y;
		return node;
	}
	
	public static Node child(Node parent, int direction) {
		Node node = new Node();
		node.parent = parent;
		switch (direction) {
		case N:
			node.g = parent.g+10;
			node.x = parent.x;
			node.y = parent.y - 1;
			break;
		case NE:
			node.g = parent.g+14;
			node.x = parent.x + 1;
			node.y = parent.y - 1;
			break;
		case E:
			node.g = parent.g+10;
			node.x = parent.x + 1;
			node.y = parent.y;
			break;
		case SE:
			node.g = parent.g+14;
			node.x = parent.x + 1;
			node.y = parent.y + 1;
			break;
		case S:
			node.g = parent.g+10;
			node.x = parent.x;
			node.y = parent.y + 1;
			break;
		case SW:
			node.g = parent.g+14;
			node.x = parent.x - 1;
			node.y = parent.y + 1;
			break;
		case W:
			node.g = parent.g+10;
			node.x = parent.x - 1;
			node.y = parent.y;
			break;
		case NW:
			node.g = parent.g+14;
			node.x = parent.x - 1;
			node.y = parent.y - 1;
			break;
		}
		return node;
	}
}
